package io.renren.modules.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.modules.usian.entity.CollegeEntity;
import io.renren.modules.usian.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.usian.dao.TeacherDao;
import io.renren.modules.usian.entity.TeacherEntity;
import io.renren.modules.usian.service.TeacherService;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private CollegeService collegeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherEntity> page = this.page(
                new Query<TeacherEntity>().getPage(params),
                new QueryWrapper<TeacherEntity>()
        );

       List<TeacherEntity> list = page.getRecords();
       list.stream().map((teacher)->{
           CollegeEntity college = collegeService.getById(teacher.getCid());
           teacher.setCname(college.getName());
           return teacher;
       }).collect(Collectors.toList());

        return new PageUtils(page);
    }

    @Override
    public List<TeacherEntity> showListByCid(int cid, int role) {
        LambdaQueryWrapper<TeacherEntity> lambdaQueryWrapper = new LambdaQueryWrapper<TeacherEntity>();
        lambdaQueryWrapper.eq(TeacherEntity::getCid,cid);
        lambdaQueryWrapper.eq(TeacherEntity::getRole,role);
        lambdaQueryWrapper.eq(TeacherEntity::getStatus,0);
        List<TeacherEntity> list = teacherDao.selectList(lambdaQueryWrapper);
        return list;
    }

}